<!DOCTYPE html>
<html>
<head>
    <title>批量导入用户</title>
</head>
<body>
    <h2>Excel 导入说明</h2>
    <ol>
        <li>下载<a href="/static/user_template.xlsx">模板文件</a></li>
        <li>文件列要求：
            <ul>
                <li>账号（必填）</li>
                <li>密码（必填，至少6位）</li>
                <li>姓名（必填）</li>
                <li>性别（可选，男/女/其他）</li>
                <li>专业班级（可选）</li>
                <li>配额（可选，数字）</li>
            </ul>
        </li>
    </ol>

    <form id="uploadForm" enctype="multipart/form-data">
        {% csrf_token %}
        <input type="file" name="excel_file" accept=".xls, .xlsx">
        <button type="button" onclick="uploadFile()">开始导入</button>
    </form>

    <div id="result"></div>

    <script>
    function uploadFile() {
        const formData = new FormData(document.getElementById('uploadForm'));

        fetch('/user/import_users_from_excel/', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            let resultHtml = `<h3>${data.message}</h3>`;
            if(data.errors.length > 0) {
                resultHtml += `<ul>${data.errors.map(e => `<li>${e}</li>`).join('')}</ul>`;
            }
            document.getElementById('result').innerHTML = resultHtml;
        })
        .catch(error => {
            console.error('Error:', error);
        });
    }
    </script>
</body>
</html>
